package com.csi.juegoweb.dao.impl;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import sun.net.www.http.Hurryable;

import com.csi.juegoweb.dao.DonacionDAO;
import com.csi.juegoweb.domain.Donacion;

/**
 * @see com.csi.juegoweb.Donacion
 * @author MyEclipse Persistence Tools
 */

@Repository("donacionDAO")
public class DonacionDAOImpl implements DonacionDAO {
	
	private static final Log log = LogFactory.getLog(DonacionDAOImpl.class);
	
	@Autowired
	HibernateTemplate hibernateTemplate;
	
	@Autowired
	private JdbcTemplate jdbcTemplateCore;
	
	public DonacionDAOImpl(){

	}
	
	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#save(com.csi.juegoweb.domain.Donacion)
	 */
	public void save(Donacion transientInstance) {		
		log.debug("saving Donacion instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#delete(com.csi.juegoweb.domain.Donacion)
	 */
	public void delete(Donacion persistentInstance) {
		log.debug("deleting Donacion instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findById(java.lang.Integer)
	 */
	public Donacion findById(java.lang.Integer id) {
		log.debug("getting Donacion instance with id: " + id);
		try {
			Donacion instance = (Donacion) getHibernateTemplate().get(
					"com.csi.juegoweb.Donacion", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findByExample(com.csi.juegoweb.domain.Donacion)
	 */
	public List<Donacion> findByExample(Donacion instance) {
		log.debug("finding Donacion instance by example");
		try {
			List<Donacion> results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	public List<Donacion> findByProperty(String propertyName, Object value) {
		log.debug("finding Donacion instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Donacion as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List<Donacion> findByProperty(String propertyNameOne, Object valueOne,
			String propertyNameTwo, Object valueTwo) {
		log.debug("finding Donacion instance with property: " + propertyNameOne
				+ ", value: " + valueOne);
		
		List<Donacion> list=null;
		try {
			String queryString = "from Donacion as model where model."
					+ propertyNameOne + "= ?   and  model."+propertyNameTwo + "= ?";
			list = getHibernateTemplate().find(queryString, valueOne, valueTwo);
		} catch (Exception ex) {
			log.error("find by property name failed", ex);
			ex.printStackTrace();
		}
		return list;
	}
	
	public Donacion findByPropertys(String propertyNameOne, Object valueOne,
			String propertyNameTwo, Object valueTwo) {
		log.debug("finding Donacion instance with property: " + propertyNameOne
				+ ", value: " + valueOne);
		try {
			String queryString = "from Donacion as model where model."
					+ propertyNameOne + "=" + valueOne + 
					"  and  model."+propertyNameTwo + "=" + valueTwo;
			
			return jdbcTemplateCore.queryForObject(queryString, Donacion.class);
			
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findByCoJugdes(java.lang.Object)
	 */
	public List<Donacion> findByCoJugdes(Object coJugdes) {
		return findByProperty(CO_JUGDES, coJugdes);
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findByFlConfirn(java.lang.Object)
	 */
	public List<Donacion> findByFlConfirn(Object flConfirn) {
		return findByProperty(FL_CONFIRN, flConfirn);
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findByNuMonto(java.lang.Object)
	 */
	public List<Donacion> findByNuMonto(Object nuMonto) {
		return findByProperty(NU_MONTO, nuMonto);
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#findAll()
	 */
	public List<Donacion> findAll() {
		log.debug("finding all Donacion instances");
		try {
			String queryString = "from Donacion";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#merge(com.csi.juegoweb.domain.Donacion)
	 */
	public Donacion merge(Donacion detachedInstance) {
		log.debug("merging Donacion instance");
		try {
			Donacion result = (Donacion) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#attachDirty(com.csi.juegoweb.domain.Donacion)
	 */
	public void attachDirty(Donacion instance) {
		log.debug("attaching dirty Donacion instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.csi.juegoweb.dao.impl.DonacionDAO#attachClean(com.csi.juegoweb.domain.Donacion)
	 */
	public void attachClean(Donacion instance) {
		log.debug("attaching clean Donacion instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public HibernateTemplate getHibernateTemplate() {
		
		return hibernateTemplate;
	}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
	
}